package com.sz.admin.admin.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.sz.admin.admin.service.AdminService;
import com.sz.admin.admin.pojo.po.Admin;
import com.sz.admin.admin.mapper.AdminMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.QueryChain;
import com.sz.core.common.enums.CommonResponseEnum;
import com.sz.core.util.PageUtils;
import com.sz.core.util.BeanCopyUtils;
import com.sz.core.util.Utils;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;
import java.io.Serializable;
import java.util.List;
import com.sz.admin.admin.pojo.dto.AdminCreateDTO;
import com.sz.admin.admin.pojo.dto.AdminUpdateDTO;
import com.sz.admin.admin.pojo.dto.AdminListDTO;
import com.sz.admin.admin.pojo.dto.AdminImportDTO;
import com.sz.core.common.entity.ImportExcelDTO;
import com.sz.excel.core.ExcelResult;
import java.io.OutputStream;
import jakarta.servlet.http.HttpServletResponse;
import com.sz.core.util.FileUtils;
import com.sz.excel.utils.ExcelUtils;
import lombok.SneakyThrows;
import com.sz.admin.admin.pojo.vo.AdminVO;

/**
 * <p>
 * 管理员 服务实现类
 * </p>
 *
 * @author sz-admin
 * @since 2025-06-27
 */
@Service
@RequiredArgsConstructor
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Override
    public void create(AdminCreateDTO dto){
        Admin admin = BeanCopyUtils.copy(dto, Admin.class);
        save(admin);
    }

    @Override
    public void update(AdminUpdateDTO dto){
        Admin admin = BeanCopyUtils.copy(dto, Admin.class);
        QueryWrapper wrapper;
        // id有效性校验
        wrapper = QueryWrapper.create()
            .eq(Admin::getAdId, dto.getAdId());
        CommonResponseEnum.INVALID_ID.assertTrue(count(wrapper) <= 0);

        saveOrUpdate(admin);
    }

    @Override
    public PageResult<AdminVO> page(AdminListDTO dto){
        Page<AdminVO> page = pageAs(PageUtils.getPage(dto), buildQueryWrapper(dto), AdminVO.class);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<AdminVO> list(AdminListDTO dto){
        return listAs(buildQueryWrapper(dto), AdminVO.class);
    }

    @Override
    public void remove(SelectIdsDTO dto){
        CommonResponseEnum.INVALID_ID.assertTrue(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public AdminVO detail(Object id){
        Admin admin = getById((Serializable) id);
        CommonResponseEnum.INVALID_ID.assertNull(admin);
        return BeanCopyUtils.copy(admin, AdminVO.class);
    }

    @SneakyThrows
    @Override
    public void importExcel(ImportExcelDTO dto) {
        ExcelResult<AdminImportDTO> excelResult = ExcelUtils.importExcel(dto.getFile().getInputStream(), AdminImportDTO.class, true);
        List<AdminImportDTO> list = excelResult.getList();
        List<String> errorList = excelResult.getErrorList();
        String analysis = excelResult.getAnalysis();
        System.out.println(" analysis : " + analysis);
        System.out.println(" isCover : " + dto.getIsCover());
    }

    @SneakyThrows
    @Override
    public void exportExcel(AdminListDTO dto, HttpServletResponse response) {
        List<AdminVO> list = list(dto);
        String fileName = "test模板";
        OutputStream os = FileUtils.getOutputStream(response, fileName + ".xlsx");
        ExcelUtils.exportExcel(list, "test", AdminVO.class, os);
    }

    private static QueryWrapper buildQueryWrapper(AdminListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(Admin.class);
        if (Utils.isNotNull(dto.getAdName())) {
            wrapper.like(Admin::getAdName, dto.getAdName());
        }
        if (Utils.isNotNull(dto.getAdNationality())) {
            wrapper.eq(Admin::getAdNationality, dto.getAdNationality());
        }
        if (Utils.isNotNull(dto.getAdPoliticalstatus())) {
            wrapper.eq(Admin::getAdPoliticalstatus, dto.getAdPoliticalstatus());
        }
        if (Utils.isNotNull(dto.getAdSuperior())) {
            wrapper.eq(Admin::getAdSuperior, dto.getAdSuperior());
        }
        if (Utils.isNotNull(dto.getAdPassword())) {
            wrapper.eq(Admin::getAdPassword, dto.getAdPassword());
        }
        if (Utils.isNotNull(dto.getAdPhone())) {
            wrapper.eq(Admin::getAdPhone, dto.getAdPhone());
        }
        if (Utils.isNotNull(dto.getAdCertificateid())) {
            wrapper.eq(Admin::getAdCertificateid, dto.getAdCertificateid());
        }
        if (Utils.isNotNull(dto.getAdSex())) {
            wrapper.eq(Admin::getAdSex, dto.getAdSex());
        }
        if (Utils.isNotNull(dto.getAdPosition())) {
            wrapper.eq(Admin::getAdPosition, dto.getAdPosition());
        }
        if (Utils.isNotNull(dto.getIsdelete())) {
            wrapper.eq(Admin::getIsdelete, dto.getIsdelete());
        }
        if (Utils.isNotNull(dto.getAdLastUpdateTime())) {
            wrapper.eq(Admin::getAdLastUpdateTime, dto.getAdLastUpdateTime());
        }
        if (Utils.isNotNull(dto.getAdCreateTime())) {
            wrapper.eq(Admin::getAdCreateTime, dto.getAdCreateTime());
        }
        return wrapper;
    }
}